<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label=":">
        <el-input v-model="queryParams.spuName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="货号:">
        <el-input v-model="queryParams.sn" size="small" placeholder="请输入货号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标题:">
        <el-input v-model="queryParams.title" size="small" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品牌ID:">
        <el-input v-model="queryParams.brandId" size="small" placeholder="请输入品牌ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品牌名称:">
        <el-input v-model="queryParams.brandName" size="small" placeholder="请输入品牌名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类ID:">
        <el-input v-model="queryParams.categoryId" size="small" placeholder="请输入分类ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类名称:">
        <el-input v-model="queryParams.categoryName" size="small" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库存:">
        <el-input v-model="queryParams.stock" size="small" placeholder="请输入库存" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="预警库存:">
        <el-input v-model="queryParams.warningStock" size="small" placeholder="请输入预警库存" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="规格列表:">
        <el-input v-model="queryParams.specs" size="small" placeholder="请输入规格列表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="参数列表:">
        <el-input v-model="queryParams.params" size="small" placeholder="请输入参数列表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="图片:">
        <el-input v-model="queryParams.image" size="small" placeholder="请输入图片" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="图片列表:">
        <el-input v-model="queryParams.images" size="small" placeholder="请输入图片列表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="型号:">
        <el-input v-model="queryParams.model" size="small" placeholder="请输入型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="介绍:">
        <el-input v-model="queryParams.introduction" size="small" placeholder="请输入介绍" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电脑端详情:">
        <el-input v-model="queryParams.detailHtml" size="small" placeholder="请输入电脑端详情" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="移动端详情:">
        <el-input v-model="queryParams.detailMobileHtml" size="small" placeholder="请输入移动端详情" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.importSupplierSiteNo" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.importSupplierName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商定价:">
        <el-input v-model="queryParams.suplierPrice" size="small" placeholder="请输入供应商定价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商定价单位 :">
        <el-input v-model="queryParams.priceUnit" size="small" placeholder="请输入供应商定价单位 " clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:basicSpu:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:basicSpu:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:basicSpu:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:basicSpu:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="spuName" />
      <el-table-column label="货号" align="center" prop="sn" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="品牌ID" align="center" prop="brandId" />
      <el-table-column label="品牌名称" align="center" prop="brandName" />
      <el-table-column label="分类ID" align="center" prop="categoryId" />
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="预警库存" align="center" prop="warningStock" />
      <el-table-column label="备货状态" align="center" prop="stockStatus" />
      <el-table-column label="规格列表" align="center" prop="specs" />
      <el-table-column label="参数列表" align="center" prop="params" />
      <el-table-column label="图片" align="center" prop="image" />
      <el-table-column label="图片列表" align="center" prop="images" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="介绍" align="center" prop="introduction" />
      <el-table-column label="电脑端详情" align="center" prop="detailHtml" />
      <el-table-column label="移动端详情" align="center" prop="detailMobileHtml" />
      <el-table-column label="" align="center" prop="importSupplierSiteNo" />
      <el-table-column label="" align="center" prop="importSupplierName" />
      <el-table-column label="供应商定价" align="center" prop="suplierPrice" />
      <el-table-column label="供应商定价单位 " align="center" prop="priceUnit" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['goods:basicSpu:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['goods:basicSpu:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="basicSpuFormDialog.title" :visible.sync="basicSpuFormDialog.visible" :close-on-click-modal="false" :before-close="resetBasicSpuFormDialog" width="500px" append-to-body>
      <el-form ref="basicSpuForm" :model="basicSpuFormData" :rules="basicSpuFormRules" label-width="80px">
        <el-form-item label="" prop="spuName">
          <el-input v-model="basicSpuFormData.spuName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="货号" prop="sn">
          <el-input v-model="basicSpuFormData.sn" placeholder="请输入货号" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="basicSpuFormData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="品牌ID" prop="brandId">
          <el-input v-model="basicSpuFormData.brandId" placeholder="请输入品牌ID" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="basicSpuFormData.brandName" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="分类ID" prop="categoryId">
          <el-input v-model="basicSpuFormData.categoryId" placeholder="请输入分类ID" />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="basicSpuFormData.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="basicSpuFormData.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="预警库存" prop="warningStock">
          <el-input v-model="basicSpuFormData.warningStock" placeholder="请输入预警库存" />
        </el-form-item>
        <el-form-item label="规格列表" prop="specs">
          <el-input v-model="basicSpuFormData.specs" placeholder="请输入规格列表" />
        </el-form-item>
        <el-form-item label="参数列表" prop="params">
          <el-input v-model="basicSpuFormData.params" placeholder="请输入参数列表" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <el-input v-model="basicSpuFormData.image" placeholder="请输入图片" />
        </el-form-item>
        <el-form-item label="图片列表" prop="images">
          <el-input v-model="basicSpuFormData.images" placeholder="请输入图片列表" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="basicSpuFormData.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input v-model="basicSpuFormData.introduction" placeholder="请输入介绍" />
        </el-form-item>
        <el-form-item label="电脑端详情" prop="detailHtml">
          <el-input v-model="basicSpuFormData.detailHtml" placeholder="请输入电脑端详情" />
        </el-form-item>
        <el-form-item label="移动端详情" prop="detailMobileHtml">
          <el-input v-model="basicSpuFormData.detailMobileHtml" placeholder="请输入移动端详情" />
        </el-form-item>
        <el-form-item label="" prop="importSupplierSiteNo">
          <el-input v-model="basicSpuFormData.importSupplierSiteNo" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="importSupplierName">
          <el-input v-model="basicSpuFormData.importSupplierName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="供应商定价" prop="suplierPrice">
          <el-input v-model="basicSpuFormData.suplierPrice" placeholder="请输入供应商定价" />
        </el-form-item>
        <el-form-item label="供应商定价单位 " prop="priceUnit">
          <el-input v-model="basicSpuFormData.priceUnit" placeholder="请输入供应商定价单位 " />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmBasicSpuFormDialog">确 定</el-button>
        <el-button @click="resetBasicSpuFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BasicSpuApi from '@/api/goods/BasicSpuApi'

const defaultBasicSpuFormData = {
  spuName: null,

  sn: null,

  title: null,

  brandId: null,

  brandName: null,

  categoryId: null,

  categoryName: null,

  stock: null,

  warningStock: null,

  stockStatus: '0',

  specs: null,

  params: null,

  image: null,

  images: null,

  model: null,

  introduction: null,

  detailHtml: null,

  detailMobileHtml: null,

  importSupplierSiteNo: null,

  importSupplierName: null,

  suplierPrice: null,

  priceUnit: null

}
export default {
  name: 'BasicSpuList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        spuName: null,
        sn: null,
        title: null,
        brandId: null,
        brandName: null,
        categoryId: null,
        categoryName: null,
        stock: null,
        warningStock: null,
        stockStatus: null,
        specs: null,
        params: null,
        image: null,
        images: null,
        model: null,
        introduction: null,
        detailHtml: null,
        detailMobileHtml: null,
        importSupplierSiteNo: null,
        importSupplierName: null,
        suplierPrice: null,
        priceUnit: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      basicSpuFormDialog: {
        title: '',
        visible: false
      },
      basicSpuFormData: Object.assign({}, defaultBasicSpuFormData),
      basicSpuFormRules: {
        stock: [{ required: true, message: '库存不能为空', trigger: 'blur' }]

      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openBasicSpuFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return BasicSpuApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openBasicSpuFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      BasicSpuApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return BasicSpuApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openBasicSpuFormDialog(id) {
      this.basicSpuFormDialog.visible = true
      if (id) {
        this.basicSpuFormDialog.title = '云仓共享仓储-修改'
        BasicSpuApi.get(id).then(response => {
          this.basicSpuFormData = {
            ...this.basicSpuFormData,
            ...response.data
          }
        })
      } else {
        this.basicSpuFormDialog.title = '云仓共享仓储-添加'
      }
    },
    confirmBasicSpuFormDialog() {
      this.$refs['basicSpuForm'].validate(valid => {
        if (valid) {
          if (this.basicSpuFormData.id) {
            BasicSpuApi.edit(this.basicSpuFormData).then(response => {
              this.$message.success('修改成功')
              this.resetBasicSpuFormDialog()
              this.handleQuery()
            })
          } else {
            BasicSpuApi.add(this.basicSpuFormData).then(response => {
              this.$message.success('添加成功')
              this.resetBasicSpuFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetBasicSpuFormDialog() {
      this.basicSpuFormDialog = {
        title: '',
        visible: false
      }
      this.basicSpuFormData = Object.assign({}, defaultBasicSpuFormData)
      this.resetForm('basicSpuForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
